const { Sequelize } = require("sequelize")
const useUser = require("./user")
const useArticle = require("./article")
const config = require("../config")
const moment = require("moment")

const sequelize = new Sequelize({
    ...config.MODEL,
    hooks: {
        beforeUpdate(instance) {
            instance.set(
                "updatedAt",
                moment().format("YYYY-MM-DD HH:mm")
            )
        }
    }
})

const User = useUser(sequelize)
const Article = useArticle(sequelize)

User.hasMany(Article)
Article.belongsTo(User)

sequelize.sync({ alter: true });
(async() => {
    const user = await User.findOne({
        where: { username: "lzq2021v3" }
    })

    if (user) {
        return
    }
    //默认账号密码
    await User.create({
        username: "lzq2021v3",
        password: "Aa888888"
    })
})()

module.exports = { User, Article }